package com.haipumi.biz.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.haipumi.beans.haipumi_product;
import com.haipumi.biz.GoodsBiz;
import com.haipumi.dao.DBHelper;

public class GoodsBizImpl implements GoodsBiz {
	private DBHelper db=new DBHelper();
	
	@Override
	public List<haipumi_product> findAllGoods() {
		String sql="select * from haipumi_product ";
		List<haipumi_product> list=null;
		try {
			list=db.finds(sql, null,haipumi_product.class);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return list;
	}
	@Override
	public int findGoodsCount(String pname) {
		List<Object> params=new ArrayList<Object>();
		String sql="select count(*) from haipumi_product";
		if(!"".equals(pname)){
			sql="select count(*) from haipumi_product where pname like ?";
			params.add("%"+pname+"%");
		}
		double r=-1;
			try {
				r=db.findDouble(sql, params);
			} catch (SQLException e) {
				e.printStackTrace();
			} 
		return (int) r;
	}
	@Override
	public haipumi_product addGoods(haipumi_product goods) {
		//TODO 将常量改为变量
		String sql="insert into haipumi_product(pname ,pnum ,pneed ,pdiscount,pphoto) values(?,?,?,?,?)";
		List<Object> params=new ArrayList<Object>();
		params.add(goods.getPname());
		params.add("100");
		params.add(goods.getPneed());
		params.add(goods.getPdiscount());
		params.add("model.jpg");
		try {
			int pid=db.insertWithKey(sql, params);
			goods.setPid(Integer.toString(pid ));
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} 
		return goods;
	}
	@Override
	public haipumi_product updateGoods(haipumi_product goods) {
		String sql="update haipumi_product set pname=?,pnum=? ,pneed=?,pdiscount=?  where pid=?";
		List<Object> params=new ArrayList<Object>();
		params.add(goods.getPname());
		params.add(goods.getPnum());
		params.add(goods.getPneed());
		params.add(goods.getPdiscount());
		params.add(goods.getPid());
		try {
			db.doUpdate(sql, params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return goods;
	}
	@Override
	public List<haipumi_product> findGoods(String pname, int rows, int page,
			String sort, String order) {
		int start=(page-1)*rows;
		String sql="";//区分是否是按名字查询
		List<Object> params=new ArrayList<Object>();
		if("".equals(pname)){
			sql="select * from haipumi_product order by ? ? limit "+start+","+rows;
			params.add(sort);
			params.add(order);
		}else{
			sql="select * from haipumi_product where pname like ? order by ? ? limit "+start+","+rows;
			params.add("%"+pname+"%");
			params.add(sort);
			params.add(order);
		}
		List<haipumi_product> list=null;
		try {
			list=db.findAll(sql, params, haipumi_product.class);
		} catch (Exception e1) {
			e1.printStackTrace();
			throw new RuntimeException();
		}
		return list;
	}
	@Override
	public haipumi_product updateGoodsStatus(haipumi_product goods) {
		String sql="update haipumi_product set pstatus=? where pid=?";
		List<Object> params=new ArrayList<Object>();
		params.add(goods.getPstatus());
		params.add(goods.getPid());
		try {
			db.doUpdate(sql, params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return goods;
	}

	@Override
	public boolean updateGoodsPic(haipumi_product goods) {
		boolean flag=false;
		String sql="update haipumi_product set pphoto=?  where pid=?";
		List<Object> params=new ArrayList<Object>();
		params.add(goods.getPphoto());
		params.add(goods.getPid());
		try {
			db.doUpdate(sql, params);
			flag=true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}
	@Override
	public haipumi_product updateGoodsDetail(haipumi_product goods) {
		String sql="update haipumi_product set pdescribe=?  where pid=?";
		List<Object> params=new ArrayList<Object>();
		params.add(goods.getPdescribe());
		params.add(goods.getPid());
		try {
			db.doUpdate(sql, params);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} 
		return goods;
	}

}
